/*!
 * Ext JS Library 3.0.0
 * Copyright(c) 2006-2009 Ext JS, LLC
 * licensing@extjs.com
 * http://www.extjs.com/license
 */
Ext.onReady(function(){
    Ext.QuickTips.init();
    var systemSetupActions = new Ext.Panel({
        frame:true,
        title: '系统设置',
        collapsible:true,
        contentEl:'system-setup',
        titleCollapse: true
    });
    if (userType != "admin"){
        systemSetupActions.setVisible(false);
    }
    var houseActions = new Ext.Panel({
        frame:true,
        scripts: true,
        title: '业主相关',
        collapsible:true,
        contentEl:'house-actions',
        titleCollapse: true
    });

    var systemServiceActions = new Ext.Panel({
        frame:true,
        title: '系统服务',
        collapsible:true,
        contentEl:'system-service',
        titleCollapse: true
    });

    var actionPanel = new Ext.Panel({
        id:'action-panel',
        region:'west',
        split:true,
        //collapsible: true,
        collapseMode: 'mini',
        width:150,
        minWidth: 150,
        border: false,
        baseCls:'x-plain',
        items: [houseActions, systemSetupActions, systemServiceActions]
    });

    //右边工作区的Panel
    var workPanel  = new Ext.Panel({
        frame:true,
        //        autoLoad: {
        //            url: '/house/findHouse',
        //            scripts: true
        //        },
        title: '工作区',
        //collapsible:true,
        titleCollapse: true,
        region:'center'
    });
    //alert(Ext.isAir);
    if(Ext.isAir){ // create AIR window
        var win = new Ext.air.MainWindow({
            layout:'border',
            items: [actionPanel, workPanel],
            title: 'Simple Tasks',
            iconCls: 'icon-show-all'
        }).render();
    }else{
        var viewport = new Ext.Viewport({
            layout:'border',
            //items: [actionPanel, grid]
            items: [actionPanel,workPanel]
        });
    }

    var ab = actionPanel.body;
    ab.on('mousedown', doAction, null, {
        delegate:'a'
    });
    ab.on('click', Ext.emptyFn, null, {
        delegate:'a',
        preventDefault:true
    });
    
    var actions = {
        'find_house' : function(){
            workPanel.setTitle("搜索业主");
            workPanel.getUpdater().update({
                url:'/house/findHouse',
                scripts:true,
                discardUrl:true,
                nocache:true,
                text:"正在载入..."
            });
        },

        'user_list' : function(){
            workPanel.setTitle("用户维护");
            workPanel.getUpdater().update({
                url:'/user/userList',
                scripts:true,
                discardUrl:true,
                nocache:true,
                text:"正在载入..."
            });
        },

        'building_list' : function(){
            workPanel.setTitle("楼座单元维护");
            workPanel.getUpdater().update({
                url:'/building/buildingList',
                scripts:true,
                discardUrl:true,
                nocache:true,
                text:"正在载入..."
            });
        },

        'price_list' : function(){
            workPanel.setTitle("价格维护");
            workPanel.getUpdater().update({
                url:'/price/priceList',
                scripts:true,
                discardUrl:true,
                nocache:true,
                text:"正在载入..."
            });
        },

        'house_list' : function(){
            workPanel.setTitle("业主维护");
            workPanel.getUpdater().update({
                url:'/house/houseList',
                scripts:true,
                discardUrl:true,
                nocache:true,
                text:"正在载入..."
            });
        },

        'water_record' : function(){
            workPanel.setTitle("水表抄表");
            workPanel.getUpdater().update({
                url:'/water_meter/waterMeterList',
                scripts:true,
                discardUrl:true,
                nocache:true,
                text:"正在载入..."
            });
        },

        'action-delete' : function(){
            Ext.Msg.confirm('Confirm', 'Are you sure you want to delete the selected task(s)?',
                function(btn){
                    if(btn == 'yes'){
                        selections.each(function(s){
                            taskStore.remove(s);
                        });
                    }
                });
        }
    };

    function doAction(e, t){
        e.stopEvent();
        actions[t.id]();
    }
});






